<template>
  <div class="bg-light">
    <!--标签区-->
    <div>
      <!--￥event（selectTags）表示子组件传过来的数据，这是固定写法。
       赋值给note.noteTags-->
      <NoteTag @getTags="selectTags = $event" :detailTags="detailTags" />
    </div>
    <!--工具栏区域-->
    <el-affix :offset="71">
      <div id="editor-toolbar" class="shadow"></div>
      <div>
        <el-button @click="saveNote" class="border-0">保存</el-button>
      </div>
    </el-affix>
    <!--内容区-->
    <div class="px-5 py-5">
      <!--标题区-->
      <div class="shadw-lg">
        <input
          type="text"
          v-model="note.noteName"
          class="note-name px-3 py-3"
          placeholder="请输入标题...."
        />
      </div>
      <!--内容输入区-->
      <div id="editor-container" class="note-content shadow-lg px-3 py-3"></div>
    </div>
  </div>
</template>

<script>
import NoteTag from "./NoteTag.vue";
import Editor from "@/tools/editor.js";
export default {
  name: "NoteEdit",
  components: {
    NoteTag,
  },
  data() {
    return {
      editor: null,
      noteId: this.$route.params.noteId,
      selectTags: [],//用于接收NoteTag.vue组件传过来的选中标签
      detailTags: [],//用给NoteTag.vue组件初始化选中标签
      //要提交到后端的数据
      note: {
        noteId: null,
        noteName: null,
        noteIntro: null,
        noteContent: null,
        noteTags: [],
      },
    };
  },
  async mounted() {
    await this.getDetail();
    this.editor = new Editor();
    this.editor.initAll(this.note.noteContent);
  },
  methods: {
    //读取详情页数据
    async getDetail() {
      await this.$axios("/api/note/detail?noteId=" + this.noteId).then(
        (response) => {
          let data = response.data;
          if (data.code == 200) {
            this.note = data.data;
            this.detailTags = data.data.Tags;
          } else {
            alert(data.msg);
          }
        }
      );
    },
    //保存笔记
    saveNote() {
      this.note.noteIntro = this.editor.getNoteIntro();
      this.note.note.noteContent = this.editor.getNoteContent();
      this.note.noteTags = this.selectTags
      //发起网络请求
      this.$axios.post("/api/space/note/edit", this.note)
      .then((response) => {
        let data = response.data;
        if (data.code == 200) {
          this.$router.push("/space/notelist");
        }
        alert(data.msg);
      });
    },
  },
};
</script>

<style>
</style>